package com.zpark.controller;

import com.zpark.exception.PasswordErrorException;
import com.zpark.exception.UserNotFoundException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;

/**
 * @PackageName: com.zpark.controller
 * @ClassName: ExceptionControllerAdivce
 * @Description:
 * @author: RZS
 * @date: 2023/4/13  15:10
 */
@ControllerAdvice
public class ExceptionControllerAdivce {

    /**
     * 全局异常处理类
     * @param e 异常对象
     * @return
     */
    @ExceptionHandler
    public ModelAndView handlerException(Exception e){
        ModelAndView view = new ModelAndView();

        // 判断异常类型
        if (e instanceof UserNotFoundException) {
            view.addObject("msg", "UserNotFoundException: " + e.getMessage());
            view.setViewName("error");
        }else if (e instanceof PasswordErrorException) {
            view.addObject("msg", "PasswordErrorException: " + e.getMessage());
            view.setViewName("error");
        }

        return view;
    }
}
